package com.example.springai.config;

import dev.langchain4j.data.document.Metadata;
import dev.langchain4j.data.embedding.Embedding;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.model.embedding.EmbeddingModel;
import dev.langchain4j.model.openai.OpenAiEmbeddingModel;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.rag.query.Query;
import dev.langchain4j.store.embedding.EmbeddingMatch;
import dev.langchain4j.store.embedding.EmbeddingSearchRequest;
import dev.langchain4j.store.embedding.EmbeddingSearchResult;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.filter.Filter;
import dev.langchain4j.store.embedding.pgvector.DefaultMetadataStorageConfig;
import dev.langchain4j.store.embedding.pgvector.MetadataStorageConfig;
import dev.langchain4j.store.embedding.pgvector.PgVectorEmbeddingStore;

import java.util.List;
import java.util.stream.Collectors;

import static dev.langchain4j.store.embedding.filter.MetadataFilterBuilder.metadataKey;

public class Pgconfig {
    public static void main(String[] args) {
        EmbeddingModel embeddingModel = OpenAiEmbeddingModel.builder()
                .baseUrl("https://api.siliconflow.cn/v1")
//                .proxy(new Proxy(Proxy.Type.HTTP,new InetSocketAddress("127.0.0.1", 7890)))
                .apiKey("sk-nrvjihoykgbjabnelziszukgkcankraqcwtvohvpcuepuyyz").modelName("Qwen/Qwen3-Embedding-8B")
                .logRequests(true).logResponses(true).dimensions(64)
                .build();


        EmbeddingStore<TextSegment> embeddingStore = PgVectorEmbeddingStore.builder()
                .host("localhost")                           // Required: Host of the PostgreSQL instance
                .port(5432)                                  // Required: Port of the PostgreSQL instance
                .database("postgres")                        // Required: Database name
                .user("root")
                .dimension(64)// Required: Database user
                .password("root")
                .useIndex(true)                             // Enable IVFFlat index
                .indexListSize(100) // Required: Database password
                .table("qwen3")
                .metadataStorageConfig(DefaultMetadataStorageConfig.defaultConfig())// Required: Table name to store embeddings
                .build();



        Embedding queryEmbedding = embeddingModel.embed("鞋子").content();
        System.out.println("queryEmbedding.dimension() = " + queryEmbedding.dimension());
        EmbeddingSearchRequest embeddingSearchRequest = EmbeddingSearchRequest.builder()
                .queryEmbedding(queryEmbedding)
                .maxResults(10)
                .build();





        EmbeddingSearchResult<TextSegment> embeddingSearchResult1 = embeddingStore.search(embeddingSearchRequest);
        List<String> resultTexts = embeddingSearchResult1.matches().stream()
                .map(match -> match.embedded().text())
                .collect(Collectors.toList());
        System.out.println("resultTexts = " + resultTexts);

//        EmbeddingMatch<TextSegment> embeddingMatch1 = embeddingSearchResult1.matches().get(0);

//        System.out.println("embeddingMatch1 = " + embeddingMatch1.embedded().metadata());
//        System.out.println(embeddingMatch1.score());
//        System.out.println(embeddingMatch1.embedded().text());
    }




}
